<?php
/**
 * CK 开发框架
 * User: Clake
 * Date: 15/6/21
 * Time: 14:23
 */

namespace CK\Util;

use CK\Core\Component;
use CK\Ex\ExNotFound;
use Whoops\Handler\Handler;
use Whoops\Handler\PlainTextHandler;
use Whoops\Handler\PrettyPageHandler;
use Whoops\Run;

/**
 * 错误处理类
 * Class Error
 *
 * @package CK\Util
 */
class Error extends Component{
    /**
     * @var Run
     */
    protected $_whoops;

    public function __construct($conf) {
        $this->_whoops = new Run();
    }

    /**
     * 设置为一般WEB错误输出模式
     */
    public function setWebPretty() {
        $webHandler = new PrettyPageHandler();
        $this->_whoops->pushHandler($webHandler);
        $this->_whoops->pushHandler([$this,'handleLog']);
    }

    /**
     * 设置字符错误输出模式
     */
    public function setWebPlainText() {
        $webHandler = new PlainTextHandler();
        $this->_whoops->pushHandler($webHandler);
        $this->_whoops->pushHandler([$this,'handleLog']);
    }

    /**
     * 设置CLI错误输出模式
     */
    public function setCliPlainText() {
        $webHandler = new PlainTextHandler();
        $this->_whoops->pushHandler($webHandler);
        $this->_whoops->pushHandler([$this,'handleLog']);
    }

    /**
     * 错误处理方法
     * @param      $error_no
     * @param      $message
     * @param null $file
     * @param null $line
     *
     * @throws \Whoops\Exception\ErrorException
     */
    public function handleError($error_no, $message, $file = null, $line = null) {
        $this->_whoops->handleError($error_no, $message, $file, $line);
    }

    /**
     * 异常处理方法
     * @param \Exception $exception
     */
    public function handleException($exception) {
        $this->_whoops->handleException($exception);
    }

    /**
     * 结果进程处理方法
     */
    public function handleShutdown() {
        $this->_whoops->handleShutdown();
    }

    /**
     * 出错日志处理
     * @param \Exception $exception
     * @param \Whoops\Exception\Inspector  $inspector
     * @param \Whoops\Run $run
     */
    public function handleLog($exception, $inspector, $run) {
        if ($exception instanceof ExNotFound) return;
        $errors = [];
        $frames = $inspector->getFrames();
        /**
         * @var $frame \Whoops\Exception\Frame
         */
        foreach ($frames as $frame) {
            $errors[] = $frame->getClass().':'.$frame->getFunction().':'
                .$frame->getFile().':'.$frame->getLine();
        }
        
        Log::inst()->error($exception->getMessage(),$errors);
    }

    public function enable() {
        if (CK_DEBUG) {
            if (CK_IS_CLI) {
                $this->setCliPlainText();
            } else {
                $this->setWebPretty();
            }
        } else {
            $this->_whoops->pushHandler([$this,'handleLog']);
        }

        // 自定义错误处理
        set_error_handler([$this,'handleError']);
        // 自定义异常处理
        set_exception_handler([$this,'handleException']);
        // 自定义进程结束方法
        register_shutdown_function([$this,'handleShutdown']);
    }
}